package org.oslims.client.uicomponents;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.HasClickHandlers;
import com.google.gwt.event.shared.HandlerRegistration;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.VerticalPanel;

public class ConfirmationBox {

	private DialogBox confirmationBox = new DialogBox(false, true);
	private Label confirmationMessage = new Label();
	private Button yesButton = new Button();
	private Button noButton = new Button();

	private HandlerRegistration handlerRegistration;

	public ConfirmationBox() {
		VerticalPanel errorBoxPanel = new VerticalPanel();
		HorizontalPanel buttonLine = new HorizontalPanel();
		buttonLine.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_CENTER);

		errorBoxPanel.add(confirmationMessage);

		yesButton.setText("Yes");
		noButton.setText("No");

		confirmationBox.setText("Question");
		buttonLine.add(yesButton);
		buttonLine.add(noButton);

		noButton.addClickHandler(new ClickHandler() {

			@Override
			public void onClick(ClickEvent event) {
				confirmationBox.hide();
			}
		});

		errorBoxPanel.add(buttonLine);

		confirmationBox.add(errorBoxPanel);
	}

	public void showQuestion(String message) {
		confirmationMessage.setText(message);
		confirmationBox.center();
	}

	public HasClickHandlers getYesButton() {
		return yesButton;
	}

	public void setHandlerRegistration(HandlerRegistration handlerRegistration) {
		this.handlerRegistration = handlerRegistration;
	}

	public void removeHandler() {
		if (this.handlerRegistration != null)
			this.handlerRegistration.removeHandler();
	}
}
